package com.resou.reader.bookshelf.adapter;


import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.resou.reader.R;
import com.resou.reader.ReSouApplication;
import com.resou.reader.bookshelf.readhistory.datasupport.BookCollection;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.PreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BookShelfAdapter extends BaseQuickAdapter<BookCollection, BaseViewHolder> {
    private static final String TAG = "BookShelfAdapterList";

    private int Type = 0;//0图强item, 1列表item, 2图强添加, 3列表添加

    public int getType() {
        return Type;
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    public void setType(int type) {
        this.Type = type;
    }


    public BookShelfAdapter(int layoutResId, @Nullable List<BookCollection> data) {
        super(layoutResId, data);
    }

    public BookShelfAdapter(@Nullable List<BookCollection> data) {
        super(data);
    }

    @Override
    protected void convert(BaseViewHolder helper, BookCollection item) {
        if (helper.getLayoutPosition() == getItemCount()) {
            if (Type == 0) {
                helper.setVisible(R.id.ll_list_item, false)
                        .setVisible(R.id.ll_grid_item, false)
                        .addOnClickListener(R.id.book_shelf_item_group);
            } else {
                helper.setVisible(R.id.ll_list_item, false)
                        .setVisible(R.id.ll_grid_item, false)
                        .addOnClickListener(R.id.book_shelf_item_group);
            }
            return;
        }
        helper.addOnClickListener(R.id.book_shelf_item_group)
                .addOnLongClickListener(R.id.book_shelf_item_group)
                .setText(R.id.tv_book_name, item.getName() + "")
                .setText(R.id.tv_reading_status, item.getHistoryChapterName() + "")
                .setText(R.id.tv_book_name1, item.getName() + "")
                .setText(R.id.tv_reading_status1, item.getHistoryChapterName() + "");
        helper.setVisible(R.id.iv_update, false);
        helper.setVisible(R.id.tv_update, false);
        if (item.getWhetherUpdate() == 0) {
            helper.setVisible(R.id.iv_update, true);
            helper.setVisible(R.id.tv_update, true);
        }
        if (item.getPicUrl() != null) {
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getPicUrl()).into((ImageView) helper.getView(R.id.iv_cover));
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getPicUrl()).into((ImageView) helper.getView(R.id.iv_cover1));
        }

        //设置recycleview的item
        //0图强item, 1列表item, 2图强添加, 3列表添加
        if (Type == 0) {
            helper.setGone(R.id.ll_grid_item, true)
                    .setGone(R.id.ll_list_item, false);
        } else if (Type == 1) {
            helper.setGone(R.id.ll_grid_item, false)
                    .setGone(R.id.ll_list_item, true);
        }
    }

    public String getUpdateStatus(int whetherUpdate) {
        if (whetherUpdate == 0) {
            return "已更新";
        }
        if (whetherUpdate == 1) {
            return "连载中";
        }
        if (whetherUpdate == 2) {
            return "已暂停";
        }
        if (whetherUpdate == 3) {
            return "已完结";
        }
        return "";
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {   // 布局是GridLayoutManager所管理
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    // 如果是Header、Footer的对象则占据spanCount的位置，否则就只占用1个位置
                    return isHeader(position) ? gridLayoutManager.getSpanCount() : 1;
                }
            });
        }
    }

    /**
     * 判断是否是Header的位置
     * 如果是Header的则返回true否则返回false
     *
     * @param position
     * @return
     */
    public boolean isHeader(int position) {
        return position == 0;
    }
}
